查看原文
其他

Compose 与原生启动性能对比

codelang 扣浪 2022-09-22

Macrobenchmark 接入

官方接入文档:macrobenchmark[1]

接入运行报错:

java.lang.RuntimeException: Baseline profiles aren't supported on this device version
at androidx.benchmark.macro.CompilationMode$Partial.broadcastBaselineProfileInstall(CompilationMode.kt:261)
at androidx.benchmark.macro.CompilationMode$Partial.compileImpl$benchmark_macro_release(CompilationMode.kt:285)
at androidx.benchmark.macro.CompilationMode.resetAndCompile$benchmark_macro_release(CompilationMode.kt:73)
at androidx.benchmark.macro.MacrobenchmarkKt.macrobenchmark(Macrobenchmark.kt:139)
at androidx.benchmark.macro.MacrobenchmarkKt.macrobenchmarkWithStartupMode(Macrobenchmark.kt:301)
at androidx.benchmark.macro.junit4.MacrobenchmarkRule.measureRepeated(MacrobenchmarkRule.kt:102)
at androidx.benchmark.macro.junit4.MacrobenchmarkRule.measureRepeated$default(MacrobenchmarkRule.kt:92)
at com.codelang.benchmark.ExampleStartupBenchmark.startup(ExampleStartupBenchmark.kt:30)

搜了下 stackoverflow[2] ,不支持 Android 12 的设备,在 issueracker[3] 上 google 也给了回复,不支持在 Android 12L 上运行,建议在小于等于 31 的 API 上运行:

由于我需要测 Compose 项目,所以根据答主所述,需要引入 profileinstall-1.2.0-alpha 版本,来覆盖掉 compose-ui 透传引入的 profileinstall 版本

测试:

此次测试运行的是 release 版本,并且每次测试结束都会让 Android Studio 静置一会,然后杀死 java 进程。

数据并不能保证很准确,仅供参考,因为在不同的电脑和环境下,都会产生不同的数据。

本次测试的启动页面是列表,原生采用 RecyclerView 实现,Compose 采用 LazyColumn 实现。

电脑配置:

  • CPU:1.4 GHz 四核 Intel Core i5
  • 内存:8 GB 2133 MHz LPDDR3

模拟器:

  • Android 11

演示代码地址:

  • https://github.com/MRwangqi/ApkAnalysis[4]

点击绿色运行按钮即可开始测试:

测试结果


原生compose
页面
冷启动数据
热启动数据
温启动数据

每组测试用例自动执行 5 遍,结果会输出这 5 次中的最小值、中值、最大值,我们取中值来对比可能比较合理点:

Compose 与原生启动性能对比.png

从本次测试结果来,Compose 的冷启动略逊于原生,两者相差 124ms,但在热启动与温启动状态下,Compose 性能又优于原生

参考资料

[1]

macrobenchmark: https://developer.android.google.cn/studio/profile/macrobenchmark-overview#bumblebee

[2]

stackoverflow: https://stackoverflow.com/questions/71174855/baseline-profiles-arent-supported-on-this-device-version

[3]

issueracker: https://issuetracker.google.com/issues/228222110?pli=1

[4]

https://github.com/MRwangqi/ApkAnalysis: https://github.com/MRwangqi/ApkAnalysis


您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存